package com.simplecontact.databases;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.simplecontact.classes.Calendar;
import com.simplecontact.classes.Contact;
import com.simplecontact.classes.GroupContact;
import com.simplecontact.classes.Groups;

public class ContactDbOpenHelper extends SQLiteOpenHelper {

	private static final String CONTACT_TABLE_CREATE =  
		"CREATE TABLE " + Contact.CONTACT_TABLE_NAME + " (" +
		Contact.ContactColumns._ID + " INTEGER PRIMARY KEY, " +
		Contact.ContactColumns.NICKNAME + " TEXT, " + Contact.ContactColumns.FIRSTNAME + " TEXT, " + 
		Contact.ContactColumns.LASTNAME + " TEXT, " + Contact.ContactColumns.EMAIL + " TEXT, " +
		Contact.ContactColumns.PHONENUMBER + " TEXT, " + Contact.ContactColumns.IMAGE + " TEXT, " + 
		Contact.ContactColumns.ADDRESS + " TEXT);";
		
	private static final String GROUP_TABLE_CREATE = 
		"CREATE TABLE " + Groups.GROUP_TABLE_NAME + " (" +
		Groups.GroupColumns._ID + " INTEGER PRIMARY KEY, " +
		Groups.GroupColumns.GROUPNAME + " TEXT);";
	 
	private static final String CALENDAR_TABLE_CREATE = 
		"CREATE TABLE " + Calendar.CALENDAR_TABLE_NAME + " (" +
		Calendar.CalendarColumns._ID + " INTEGER PRIMARY KEY, " +
		Calendar.CalendarColumns.DATETIME + " INT, " + Calendar.CalendarColumns.CONTENT + " TEXT, " + Calendar.CalendarColumns.URLMUSIC + " TEXT);";
	
	private static final String GROUP_CONTACT_TABLE_CREATE = 
		"CREATE TABLE " + GroupContact.GROUP_CONTACT_TABLE_NAME + " (" +
		GroupContact.GroupContactColumns.GROUPID + " INTEGER, " +
		GroupContact.GroupContactColumns.CONTACTID + " INTEGER, " + 
		"PRIMARY KEY (" + GroupContact.GroupContactColumns.GROUPID + ", " + GroupContact.GroupContactColumns.CONTACTID + ")," + 
		"FOREIGN KEY (" + GroupContact.GroupContactColumns.GROUPID + ") references " + Groups.GROUP_TABLE_NAME + "(" + Groups.GroupColumns._ID + ")," + 
		"FOREIGN KEY (" + GroupContact.GroupContactColumns.CONTACTID + ") references " + Contact.CONTACT_TABLE_NAME + "(" + Contact.ContactColumns._ID + "));";
	
	
	public ContactDbOpenHelper(Context context, String name, CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {		
		db.execSQL(CONTACT_TABLE_CREATE);
		db.execSQL(GROUP_TABLE_CREATE);
		db.execSQL(CALENDAR_TABLE_CREATE);
		db.execSQL(GROUP_CONTACT_TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	
	}
}
